package cn.tedu.charging.order.mqtt;

import lombok.extern.slf4j.Slf4j;
import org.eclipse.paho.client.mqttv3.MqttClient;
import org.eclipse.paho.client.mqttv3.MqttException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import java.nio.charset.StandardCharsets;


@Slf4j
@Component
public class MqttProducer {
    @Autowired
    private MqttClient mqttClient;
    public boolean sendMessage(String topic, String message){
        log.debug("向主题:{},发送消息:{}",topic,message);
        try {
            mqttClient.publish(topic,message.getBytes(StandardCharsets.UTF_8),0,false);
        } catch (MqttException e) {
            log.error("向主题:{},消息发送{}失败:",topic,message,e);
        }
        return true;
    }















}
